package com.unc.allergyex.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.unc.allergyex.R;
import com.unc.allergyex.objects.db.Dish;

/**This is the expandable list adapter for the results activity. 
 * It is used by the Results activity to display the menu.
 * It uses the restaurant menu from the ObjManager to populate the categories and the items. 
 * It uses the category.xml and expanded_item.xml layout files.
 * @author Stephanie
 *
 */
public class DishAdapter extends BaseExpandableListAdapter {

	//necessary for automatic methods
	private final int MAX_ITEMS_PER_CATEGORY = 2048;
	
	//used to make the indicator icon change drawables when the list is expanded or contracted
	private static final int[] EMPTY_STATE_SET = {};
    private static final int[] GROUP_EXPANDED_STATE_SET =
            {android.R.attr.state_expanded};
	private static final int[][] GROUP_STATE_SETS = {
        EMPTY_STATE_SET, // 0
        GROUP_EXPANDED_STATE_SET // 1
	};
	
	private ArrayList<String> categories;
	private ArrayList<ArrayList<Dish>> items;
	private LayoutInflater inflater;
	
	/**The constructor for the adapter.
	 * @param context the context into which the notifications should be posted
	 * @param categories the list of category objects
	 * @param items the list of lists of dish objects
	 */
	public DishAdapter(Context context,
						   ArrayList<String> categories,
						   ArrayList<ArrayList<Dish>> items) {
		this.items = items;
		this.categories = categories;
		this.inflater = LayoutInflater.from(context);
	}
	
	/**Auto-generated method necessary for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getChild(int, int)
	 */
	@Override
	public Object getChild(int categoryIndex, int itemIndex) {
		return items.get(categoryIndex).get(itemIndex);
	}

	/**Auto-generated method necessary for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getChildId(int, int)
	 */
	@Override
	public long getChildId(int categoryIndex, int childIndex) {
		return (long) (categoryIndex*MAX_ITEMS_PER_CATEGORY + childIndex);
	}

	/**Method for creating the view for a expanded item.  Uses the expanded_item.xml file.  Inflate the view,
	 * set the header text, set the icon to an ex if not edible and check if edible. 
	 * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getChildView(int categoryIndex, int itemIndex,
			boolean isLastChild, View convertView, ViewGroup parent) {
		//inflate the view
		View v;  
		if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.expanded_item, parent, false); 
		
		//set the header text to be the dish name
		Dish item = (Dish)getChild( categoryIndex, itemIndex );
		TextView name = (TextView)v.findViewById( R.id.expanded_name );
		if( name != null )
			name.setText( item.getName() );
		
		//set the icon to an ex if not edible and a check if edible
		ImageView edible = (ImageView)v.findViewById( R.id.edible );
		if ( item.getAllergic() ){
			edible.setImageResource(R.drawable.ex);
		}
		else {
			edible.setImageResource(R.drawable.check); //need to make sure that we draw in either case because otherwise the list items won't be the same size
		}
        return v;
	}

	/**Auto-generated method necessary for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getChildrenCount(int)
	 */
	@Override
	public int getChildrenCount(int categoryIndex) {
		return items.get( categoryIndex ).size();
	}

	/**Auto-generated method necessary for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getGroup(int)
	 */
	@Override
	public Object getGroup(int categoryIndex) {
		return categories.get(categoryIndex);
	}

	/**Auto-generated method necessary for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getGroupCount()
	 */
	@Override
	public int getGroupCount() {
		return categories.size();
	}

	/**Auto-generated method necessary for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#getGroupId(int)
	 */
	@Override
	public long getGroupId(int categoryIndex) {
		return (long) (categoryIndex*MAX_ITEMS_PER_CATEGORY);
	}

	/**Method for generating layouts for the group headers. Uses the category.xml file. 
	 * Inflates the textview, sets the header text, and sets the behavior for the indicator icon. 
	 * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		//inflate the view
		View v = null;
	    if( convertView != null )
	    	v = convertView;
	    else
	    	v = inflater.inflate(R.layout.category, parent, false); 
	    
	    //set the header textview to be the dish name
	    String categoryName = (String) getGroup( groupPosition );
	    TextView category = (TextView)v.findViewById( R.id.categoryName );
		if( category != null )
			category.setText( categoryName );
		
		//set behavior for the indicator icon
		ImageView ind = (ImageView) v.findViewById( R.id.explist_indicator);
		int stateSetIndex = ( isExpanded ? 1 : 0) ;
		Drawable drawable = ind.getDrawable();
		drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
	    return v;
	}

	/**Auto-generated method necessary for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#hasStableIds()
	 */
	@Override
	public boolean hasStableIds() {
		return true;
	}

	/**Auto-generated method necessary for ExpandableListAdapter
	 * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
	 */
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
